一、短線資料(5分線)
讀取需要的Libiary
import datetime
import warnings
import pandas as pd
import requests
from pandas.core.common import SettingWithCopyWarning
warnings.simplefilter(action="ignore", category=SettingWithCopyWarning)
pd.set_option("display.max_columns", None)
撈取Tick資料
stock_date = datetime.datetime(2019, 1, 1, 0, 0)
url = "https://api.finmindtrade.com/api/v4/data"
parameter = {
"dataset": "TaiwanStockPriceTick",
"start_date": stock_date.strftime("%Y-%m-%d"),
"data_id": 2330,
}
data = requests.get(url, params=parameter)
data = data.json()
if data["status"] != 200:
raise ("Return Error")
if len(data["data"]) == 0:
continue
df = pd.DataFrame(data["data"])
指定盤中資料、合併時間欄位
df = df.loc[
(df["Time"] >= "10:00:00") & (df["Time"] <= "13:00:00")
]
df.loc[:, "date"] = pd.to_datetime(
df["date"] + " " + df["Time"]
)
df.drop(columns=["Time"], inplace=True)
合併5分內資料
df = df.set_index("date")
deal_price = df["deal_price"].resample("5T").last().ffill()
volume = df["volume"].resample("5T").sum()
df = pd.concat([deal_price, volume], axis=1).reset_index()